| Conditions | 2 |
| Paths | 8 |
| Total Lines | 102 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | ////////////////////////////////////////////////////////////////////////////////////// |
||
| 144 | function draw(str, to, first, box) { |
||
| 145 | tag || init() |
||
| 146 | |||
| 147 | var re = /("(?:((?:https?|file):\/\/(?:\\?\S)+?)|(?:\\?.)*?)")\s*(:?)|-?\d+\.?\d*(?:e[+-]?\d+)?|true|false|null|[[\]{},]|(\S[^-[\]{},"\d]*)/gi |
||
| 148 | , node = div.cloneNode() |
||
| 149 | , link = document.createElement("a") |
||
| 150 | , span = document.createElement("span") |
||
| 151 | , info = document.createElement("i") |
||
| 152 | , colon = document.createTextNode(": ") |
||
| 153 | , comma = fragment(",") |
||
| 154 | , path = [] |
||
| 155 | , cache = { |
||
| 156 | "{": fragment("{", "}"), |
||
| 157 | "[": fragment("[", "]") |
||
| 158 | } |
||
| 159 | |||
| 160 | node.className = "R" + rand + (box ? " " + box : "") |
||
| 161 | |||
| 162 | link.classList.add("L" + rand) |
||
| 163 | info.classList.add("I" + rand) |
||
| 164 | |||
| 165 | to.addEventListener("click", function(e) { |
||
| 166 | var target = e.target |
||
| 167 | , open = target.classList.contains(COLL) |
||
| 168 | if (target.tagName == "I") { |
||
| 169 | if (e.altKey) { |
||
| 170 | changeSiblings(target, COLL, !open) |
||
| 171 | } else if (e[mod]) { |
||
| 172 | open = target.nextSibling.querySelector("i") |
||
| 173 | if (open) change(target.nextSibling, "i", COLL, !open.classList.contains(COLL)) |
||
| 174 | } else { |
||
| 175 | target.classList[open ? "remove" : "add"](COLL) |
||
| 176 | } |
||
| 177 | } |
||
| 178 | }, true) |
||
| 179 | |||
| 180 | to.replaceChild(box = node, first) |
||
| 181 | loop(str, re) |
||
| 182 | |||
| 183 | function loop(str, re) { |
||
| 184 | var match, val, tmp |
||
| 185 | , i = 0 |
||
| 186 | , len = str.length |
||
| 187 | try { |
||
| 188 | for (; match = re.exec(str); ) { |
||
| 189 | val = match[0] |
||
| 190 | if (val == "{" || val == "[") { |
||
| 191 | path.push(node) |
||
| 192 | node.appendChild(cache[val].cloneNode(true)) |
||
| 193 | node = node.lastChild.previousSibling |
||
| 194 | node.len = 1 |
||
| 195 | node.start = re.lastIndex |
||
| 196 | } else if ((val == "}" || val == "]") && node.len) { |
||
| 197 | if (node.childNodes.length) { |
||
| 198 | tmp = info.cloneNode() |
||
| 199 | tmp.dataset.content = node.len + ( |
||
| 200 | node.len == 1 ? |
||
| 201 | (val == "]" ? " item, " : " property, ") : |
||
| 202 | (val == "]" ? " items, " : " properties, ") |
||
| 203 | ) + units(re.lastIndex - node.start + 1) |
||
| 204 | |||
| 205 | if ((val = node.previousElementSibling) && val.className == KEY) { |
||
| 206 | tmp.dataset.key = val.textContent.slice(1, -1).replace(/'/, "\\'") |
||
| 207 | } |
||
| 208 | node.parentNode.insertBefore(tmp, node) |
||
| 209 | } else { |
||
| 210 | node.parentNode.removeChild(node) |
||
| 211 | } |
||
| 212 | node = path.pop() |
||
| 213 | } else if (val == ",") { |
||
| 214 | node.len += 1 |
||
| 215 | node.appendChild(comma.cloneNode(true)) |
||
| 216 | } else { |
||
| 217 | if (match[2]) { |
||
| 218 | tmp = link.cloneNode() |
||
| 219 | tmp.href = match[2].replace(/\\"/g, '"') |
||
| 220 | } else { |
||
| 221 | tmp = span.cloneNode() |
||
| 222 | } |
||
| 223 | tmp.textContent = match[1] || val |
||
| 224 | tmp.classList.add(match[3] ? KEY : match[1] ? STR : match[4] ? ERR : BOOL) |
||
| 225 | node.appendChild(tmp) |
||
| 226 | if (match[3]) { |
||
| 227 | node.appendChild(colon.cloneNode()) |
||
| 228 | } |
||
| 229 | } |
||
| 230 | if (++i > 1000) { |
||
| 231 | document.title = (0|(100*re.lastIndex/len)) + "% of " + units(len) |
||
| 232 | return setTimeout(function() { loop(str, re) }) |
||
| 233 | } |
||
| 234 | } |
||
| 235 | document.title = "" |
||
| 236 | JSON.parse(str) |
||
| 237 | |||
| 238 | } catch(e) { |
||
| 239 | tmp = document.createElement("h3") |
||
| 240 | tmp.className = ERR |
||
| 241 | tmp.textContent = e |
||
| 242 | box.insertBefore(tmp, box.firstChild) |
||
| 243 | } |
||
| 244 | } |
||
| 245 | } |
||
| 246 | |||
| 284 |
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.
To learn more about declaring variables in Javascript, see the MDN.